/**
 * 
 */
package com.flying.dcss.userpermissions.action;

import java.io.PrintWriter;
import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpSession;
import javax.sound.sampled.AudioFormat.Encoding;

import org.apache.struts2.ServletActionContext;

import com.flying.dcss.app.CommonHelper;
import com.flying.dcss.app.MD5Helper;
import com.flying.dcss.app.SessionName;
import com.flying.dcss.common.service.impl.MailService;
import com.flying.dcss.common.service.impl.SmsService;
import com.flying.dcss.userpermissions.bo.impl.DcssLoginRecord;
import com.flying.dcss.userpermissions.bo.impl.DcssUser;
import com.flying.dcss.userpermissions.service.impl.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.xml.internal.bind.v2.runtime.output.Encoded;

// TODO: Auto-generated Javadoc
/**
 * The Class LoginAction.
 * 
 * @author EvanHong
 */
public class LoginAction extends ActionSupport {

	/** The user. */
	private DcssUser user;

	/** The user service. */
	private UserService userService;

	/** The mail service. */
	private MailService mailService;

	private SmsService smsService;

	public void setSmsService(SmsService smsService) {
		this.smsService = smsService;
	}

	/**
	 * Sets the mail service.
	 * 
	 * @param mailService
	 *            the new mail service
	 */
	public void setMailService(MailService mailService) {
		this.mailService = mailService;
	}

	/**
	 * Sets the user.
	 * 
	 * @param user
	 *            the new user
	 */
	public void setUser(DcssUser user) {
		this.user = user;
	}

	/**
	 * Gets the user.
	 * 
	 * @return the user
	 */
	public DcssUser getUser() {
		return user;
	}

	/**
	 * Sets the user service.
	 * 
	 * @param userService
	 *            the new user service
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	public String execute() throws Exception {

		if (userService.isValidUser(user)) {
			if (userService.getUserByEmail(user.getEmail())
					.getIsEmailVerified() == 1) {
				// Get user.
				DcssUser realUser = userService.getUserByEmail(user.getEmail());

				// Get session.
				HttpSession session = ServletActionContext.getRequest()
						.getSession();
				// Write session.
				session.setAttribute(SessionName.UserSession, realUser);
				// Set max time.
				session.setMaxInactiveInterval(900);

				// Login Record.
				DcssLoginRecord loginRecord = new DcssLoginRecord(UUID
						.randomUUID().toString(), realUser.getId(),
						ServletActionContext.getRequest().getRemoteAddr(),
						true, new Date());
				userService.addLoginRecord(loginRecord);

				// smsService.sendTestSMS("+8615072311876", "Test!");
				
				// smsService.checkSMS();

				return this.SUCCESS;
			} else {

				// Verify process.
				String codeString = CommonHelper.getShortDate(userService
						.getUserByEmail(user.getEmail()).getCreateTime())
						+ CommonHelper.getShortDate(new Date())
						+ user.getEmail();

				String verifyCode = MD5Helper.MD5Encode(codeString);

				mailService.sendVerificationEmail(user.getEmail(), verifyCode);

				// Message.
				PrintWriter out = ServletActionContext.getResponse()
						.getWriter();
				out
						.print("<script>alert('Email is not verified!The verify email is resend!');self.location='login.jsp';</script>");
				out.flush();
				out.close();
				return this.ERROR;
			}
		} else {
			PrintWriter out = ServletActionContext.getResponse().getWriter();
			out
					.print("<script>alert('Email or password is incorrect!');self.location='login.jsp';</script>");
			out.flush();
			out.close();
			return this.ERROR;
		}
	}
}
